<?php

namespace app\logic\model\entity;

use app\logic\model\LogicModel;
use think\model\concern\SoftDelete;

class EntityDjEquipmentTypeLogicModel extends LogicModel
{

    use SoftDelete;
    protected $name='entity_dj_equipment_type';

    protected $delete_time = 'delete_time';
    public function getList(\think\Request $request, array $where, string $field='*', $page = 1, $limit=15,$order='sort desc,id desc')
    {
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'title')){
                    $q->whereLike("title","%{$request['title']}%");
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query->field($field)->order($order)->page($page,$limit)->select();
        $count = $query->count();
        return [$list,$count];
    }


    public function getTree($check_status = false,$field='*')
    {
        $sort = 'sort desc,id desc';
        $one = $this->where('pid',0)
            ->where(function($q) use ($check_status){
                if($check_status){
                    $q->where('status',1);
                }
            })
            ->field($field)
            ->order($sort)->select();
        foreach ($one as &$val){
            $val['child'] = $this->where('pid',$val['id'])
                ->where(function($q) use ($check_status){
                    if($check_status){
                        $q->where('status',1);
                    }
                })
                ->field($field)
                ->order($sort)->select();

        }
        unset($val);
        return $one;
    }

}